package com.smart.smartdata;

import android.graphics.DashPathEffect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.smart.smartdata.models.UpdateDataModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class RecorderDetailActivity extends AppCompatActivity {

    @Bind(R.id.chart_heart)
    LineChart chart_heart;
    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.iv_data_export)
    ImageView ivDataExport;
    @Bind(R.id.chart_temper)
    LineChart chartTemper;
    @Bind(R.id.chart_brain)
    LineChart chartBrain;
    @Bind(R.id.tv_blood)
    TextView tvBlood;
    @Bind(R.id.chart_muscle)
    LineChart chartMuscle;

    private MessageData messageData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recoder_detail);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        StatusBarUtil.setTranslucentStatus(this);//透明状态栏

        messageData = (MessageData)getIntent().getSerializableExtra("messageData");

        ButterKnife.bind(this);

        tvName.setText(messageData.name);
        if(null !=messageData.o2 && !"".equals(messageData.o2)){

            tvBlood.setText(messageData.o2+"%");
        }else {
            tvBlood.setText("-- %");

        }

        initChart(chart_heart);

        initChartTemp(chartTemper);

        setTempData();

        List<UpdateDataModel> models = messageData.models;
        if(null != models && !models.isEmpty()){
            for(int i = 0;i < models.size(); ++i){

                reflushTemperData(chartTemper,models.get(i));
            }
        }

        initChart3(chartBrain);
        initChart4(chartMuscle);

        chart_heart.setData(generateDataLine(100));
       // chartTemper.setData(generateDataLine2(100));
        chartBrain.setData(generateDataLine3(100));
        chartMuscle.setData(generateDataLine4(100));


    }

    private void setTempData() {


        ArrayList<Entry> values1 = new ArrayList<>();


        LineDataSet d1 = new LineDataSet(values1, "");
        d1.setDrawValues(false);
        d1.setDrawIcons(false);
        d1.setLineWidth(0.5f);
        d1.setColor(0xff5BC6EE);
        d1.setDrawCircles(false);
        d1.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(d1);

        LineData lineData = new LineData(sets);
        chartTemper.setData(lineData);


    }


    private void reflushTemperData(LineChart chart, UpdateDataModel dataModel) {
        LineData data = chart.getData();
        if (data != null) {

            ILineDataSet set = data.getDataSetByIndex(0);

            if (dataModel.data != null || !dataModel.data.equals("")) {

                data.addEntry(new Entry(set.getEntryCount(), Float.valueOf(dataModel.data)), 0);

            } else {
                data.addEntry(new Entry(set.getEntryCount(), Float.valueOf(0)), 0);

            }

            data.notifyDataChanged();

            // let the chart know it's data has changed
            chart.notifyDataSetChanged();

            // limit the number of visible entries
            chart.setVisibleXRangeMaximum(120);
            // chart.setVisibleYRange(30, AxisDependency.LEFT);

            // move to the latest entry
            chart.moveViewToX(data.getEntryCount());

        }
    }


    private void initChartTemp(LineChart chart) {
        chart.setNoDataText("暂无数据");
        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(true);
        xAxis.setAxisLineColor(0xffE3ECF0);
        xAxis.setTextSize(6);
        xAxis.setTextColor(0xff878F99);


        YAxis leftAxis = chart.getAxisLeft();

        // leftAxis.setLabelCount(5, false);
        // leftAxis.setAxisMinimum(10f); // this replaces setStartAtZero(true)
        leftAxis.setAxisLineColor(0xff878F99);
        leftAxis.setTextColor(0xff878F99);
        leftAxis.setTextSize(6);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);


        // set data


        // do not forget to refresh the chart
        // holder.chart.invalidate();
        chart.animateX(750);

        chart.getLegend().setEnabled(false); // 隐藏底部图例
        chart.getDescription().setText("时间");
        chart.getDescription().setTextColor(0xff7c8589);
        chart.getDescription().setTextSize(8);
        chart.getDescription().setEnabled(true);

    }



    private void initChart(LineChart chart) {
        chart.setNoDataText("暂无数据");
        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawAxisLine(true);
        xAxis.setAxisLineColor(0xffE3ECF0);
        xAxis.setTextSize(6);
        xAxis.setTextColor(0xff878F99);


        YAxis leftAxis = chart.getAxisLeft();

        leftAxis.setLabelCount(5, false);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        leftAxis.setAxisLineColor(0xff878F99);
        leftAxis.setTextColor(0xff878F99);
        leftAxis.setTextSize(6);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);


        // set data


        // do not forget to refresh the chart
        // holder.chart.invalidate();
        chart.animateX(750);

        chart.getLegend().setEnabled(false); // 隐藏底部图例
        chart.getDescription().setText("时间");
        chart.getDescription().setTextColor(0xff7c8589);
        chart.getDescription().setTextSize(16);
        chart.getDescription().setEnabled(true);

    }

    private void initChart3(LineChart chart) {
        chart.setNoDataText("暂无数据");
        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);


        // 设置网格线的样式为虚线
        xAxis.setGridDashedLine(new DashPathEffect(new float[]{5f, 5f}, 0f));

        // 设置是否绘制轴线
        xAxis.setDrawAxisLine(true);

        xAxis.setAxisLineColor(0xffE3ECF0);
        xAxis.setTextSize(6);

        // 设置轴线的颜色
        xAxis.setTextColor(0xff878F99);

        // 设置轴线的宽度
        xAxis.setAxisLineWidth(2f);


        YAxis leftAxis = chart.getAxisLeft();

        leftAxis.setLabelCount(5, false);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        leftAxis.setAxisLineColor(0xff878F99);
        leftAxis.setTextColor(0xff878F99);
        leftAxis.setTextSize(6);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);


        // set data


        // do not forget to refresh the chart
        // holder.chart.invalidate();
        chart.animateX(750);

        chart.getLegend().setEnabled(false); // 隐藏底部图例
        chart.getDescription().setText("时间");
        chart.getDescription().setTextColor(0xff7c8589);
        chart.getDescription().setTextSize(16);
        chart.getDescription().setEnabled(true);

    }
    private void initChart4(LineChart chart) {
        chart.setNoDataText("暂无数据");
        chart.setDrawGridBackground(false);

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

        // 设置是否绘制网格线
        xAxis.setDrawGridLines(true);

        // 设置网格线的样式为虚线
        xAxis.setGridDashedLine(new DashPathEffect(new float[]{5f, 5f}, 0f));




        xAxis.setAxisLineColor(0xffE3ECF0);
        xAxis.setTextSize(6);

        // 设置轴线文字的颜色
        xAxis.setTextColor(0xff878F99);

        // 设置轴线的宽度
        xAxis.setAxisLineWidth(2f);


        YAxis leftAxis = chart.getAxisLeft();

        leftAxis.setLabelCount(5, false);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        // 设置轴线的颜色
        leftAxis.setAxisLineColor(0xff878F99);

        leftAxis.setTextColor(0xff878F99);

        leftAxis.setTextSize(6);

        // 设置网格线的样式为虚线
        leftAxis.setGridDashedLine(new DashPathEffect(new float[]{5f, 5f}, 0f));

        // 设置是否绘制轴线
        leftAxis.setDrawAxisLine(true);

        YAxis rightAxis = chart.getAxisRight();
        rightAxis.setEnabled(false);


        // set data


        // do not forget to refresh the chart
        // holder.chart.invalidate();
        chart.animateX(750);

        chart.getLegend().setEnabled(false); // 隐藏底部图例
        chart.getDescription().setText("时间");
        chart.getDescription().setTextColor(0xff7c8589);
        chart.getDescription().setTextSize(16);
        chart.getDescription().setEnabled(true);

    }

    private LineData generateDataLine(int cnt) {

        ArrayList<Entry> values1 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values1.add(new Entry(i, (int) (Math.random() * 65) + 40));
        }

        LineDataSet d1 = new LineDataSet(values1, "New DataSet " + cnt + ", (1)");
        d1.setDrawValues(false);
        d1.setDrawIcons(false);
        d1.setLineWidth(0.5f);
        d1.setColor(0xffF5555C);
        d1.setDrawCircles(false);
        d1.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<Entry> values2 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values2.add(new Entry(i, values1.get(i).getY() - 30));
        }

        LineDataSet d2 = new LineDataSet(values2, "New DataSet " + cnt + ", (2)");
        d2.setLineWidth(0.5f);
        d2.setDrawValues(false);
        d2.setColor(0xffE68C5C);
        d2.setDrawCircles(false);
        d2.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(d1);
        sets.add(d2);

        return new LineData(sets);
    }

    private LineData generateDataLine3(int cnt) {

        ArrayList<Entry> values1 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values1.add(new Entry(i, (int) (Math.random() * 65) + 40));
        }

        LineDataSet d1 = new LineDataSet(values1, "New DataSet " + cnt + ", (1)");
        d1.setDrawValues(false);
        d1.setDrawIcons(false);
        d1.setLineWidth(0.5f);
        d1.setColor(0xffE30708);
        d1.setDrawCircles(false);
        d1.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<Entry> values2 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values2.add(new Entry(i, values1.get(i).getY() - 30));
        }

        LineDataSet d2 = new LineDataSet(values2, "New DataSet " + cnt + ", (2)");
        d2.setLineWidth(0.5f);
        d2.setDrawValues(false);
        d2.setColor(0xff0000F0);
        d2.setDrawCircles(false);
        d2.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<Entry> values3 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values3.add(new Entry(i, values1.get(i).getY() - 15));
        }

        LineDataSet d3 = new LineDataSet(values3, "New DataSet " + cnt + ", (2)");
        d3.setLineWidth(0.5f);
        d3.setDrawValues(false);
        d3.setColor(0xff101113);
        d3.setDrawCircles(false);
        d3.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(d1);
        sets.add(d2);
        sets.add(d3);

        return new LineData(sets);
    }

    private LineData generateDataLine2(int cnt) {

        ArrayList<Entry> values1 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values1.add(new Entry(i, (int) (Math.random() * 65) + 40));
        }

        LineDataSet d1 = new LineDataSet(values1, "New DataSet " + cnt + ", (1)");
        d1.setDrawValues(false);
        d1.setDrawIcons(false);
        d1.setLineWidth(0.5f);
        d1.setColor(0xff5BC6EE);
        d1.setDrawCircles(false);
        d1.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(d1);

        return new LineData(sets);
    }
    private LineData generateDataLine4(int cnt) {

        ArrayList<Entry> values1 = new ArrayList<>();

        for (int i = 0; i < 12; i++) {
            values1.add(new Entry(i, (int) (Math.random() * 65) + 40));
        }

        LineDataSet d1 = new LineDataSet(values1, "New DataSet " + cnt + ", (1)");
        d1.setDrawValues(false);
        d1.setDrawIcons(false);
        d1.setLineWidth(0.5f);
        d1.setColor(0xff76508B);
        d1.setDrawCircles(false);
        d1.setMode(LineDataSet.Mode.CUBIC_BEZIER); // 设置为平滑曲线模


        ArrayList<ILineDataSet> sets = new ArrayList<>();
        sets.add(d1);

        return new LineData(sets);
    }


    @OnClick({R.id.iv_back, R.id.iv_data_export})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_data_export:
                break;
        }
    }
}
